package org.osomit.sacct.security.otp.impl;

import org.osomit.sacct.security.otp.impl.XMLOTPTokenReader;
import org.osomit.sacct.security.otp.impl.XMLOTPTokenGenerator;
import org.osomit.sacct.security.otp.iface.OTPToken;
import org.junit.BeforeClass;
import org.junit.AfterClass;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

/**
 * @author Jian Fang (John.Jian.Fang@gmail.com)
 *
 * Date: Jun 26, 2009
 */
public class XMLOTPTokenReader_UT {
	private static String SECRET_STRING = "12345678901234567890";
	private static long initCount = 100;
	private static int codeDigits = 9;
	private static boolean addChecksum = true;
	private static int truncationOffset = 20;
	private static String SN = "321151351351";
	private static String password = "good4you";
	private static int stepSize = 100;
	private static XMLOTPTokenReader reader;

	@BeforeClass
	public static void singleSetup() throws Exception{
		String result = XMLOTPTokenGenerator.generateToken(SN, SECRET_STRING, initCount,
				System.currentTimeMillis(), codeDigits, addChecksum, truncationOffset, stepSize, password);
		reader = new XMLOTPTokenReader();
		reader.setPassword(password);
		reader.setTokenAsString(result);
	}

	@AfterClass
	public static void singleTearDown() {
		reader = null;
	}

	@Test
	public void testReadToken(){
		try {
			OTPToken token = reader.read();
			assertEquals(SN, token.getSN());
			for (int i = 0; i < 10; i++) {
				String otp = token.generateOTP();
				System.out.println("Count " + (initCount + i) + " OTP " + otp);
			}

		} catch (Exception e) {
			e.printStackTrace();
			fail(e.getMessage());
		}
	}
}
